﻿using System.IO;
using UtilZ.Dotnet.Ex.Base;

namespace UtilZ.Dotnet.Ex.Model
{
    /// <summary>
    /// UDP包数据源
    /// </summary>
    public class UdpDataSource : DataSource
    {
        private readonly MemoryStream _memoryStream;
        private readonly BinaryReader _udpDataReader;
        /// <summary>
        /// 设备数据二进制读取器
        /// </summary>
        public BinaryReader UDPDataReader
        {
            get
            {
                return this._udpDataReader;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public UdpDataSource(byte[] buffer)
            : base(buffer)
        {
            this._memoryStream = new MemoryStream(buffer);
            this._udpDataReader = new BinaryReader(this._memoryStream);
        }

        /// <summary>
        /// 创建一个设备udp缓存数据对象
        /// </summary>
        /// <returns></returns>
        public static UdpDataSource Create()
        {
            return new UdpDataSource(new byte[NetHelper.UDP_PACKAGE_MAX]);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            this._udpDataReader.Dispose();
        }
    }
}
